package cn.xxc.org.controller;

import cn.xxc.basic.util.AjaxResult;
import cn.xxc.basic.util.PageList;
import cn.xxc.org.domain.Employee;
import cn.xxc.org.query.EmployeeQuery;
import cn.xxc.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @GetMapping
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    @GetMapping("/{id}")
    public Employee  findOne(@PathVariable Long id){
        return employeeService.loadByid(id);
    }

    @PutMapping
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false, "系统繁忙，请重试！！");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false, "系统繁忙，请重试！！");
        }
    }

    @PostMapping
    public PageList<Employee> queryList(@RequestBody EmployeeQuery employeeQuery){
        try {
            Integer totals=employeeService.queryCount(employeeQuery);
            List<Employee> data=employeeService.queryData(employeeQuery);
//            System.out.println(data);
            return new PageList<>(totals,data);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return new PageList<>();
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            employeeService.patchDel(ids);
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false, "系统繁忙，请重试！！");
        }
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.loadByshopId(shopId);
    }
}
